#include <iostream>

#include "../Data/DataCenter.h"
#include "../Data/UIData.h"
#include "../Data/ModelData.h"
#include "../Design/Design.h"
#include "../Visualization/VTKRenderer.h"
#include "../Model/OCCTModel.h"

#include "CommandReceiver.h"

using namespace std;

void CommandReceiver::executeTrapezoid(
    const string &name, const string &width, const string &height, const string &theta)
{ 
    // trapezoid -name=a -width=8 -height=2 -theta=30
    DataCenter& dataCenter = DataCenter::getInstance();
    
    // UI数据
    UITrapezoidData* UiTData = dynamic_cast<UITrapezoidData*>(dataCenter.GetUIData(name));    
    if(UiTData)
    {
        // 更新数据即可
        UiTData->m_Width = stod(width);
        UiTData->m_Height = stod(height);
        UiTData->m_Theta = stod(theta);
    }
    else
    {
        // 创建数据
        UiTData = new UITrapezoidData();
        UiTData->m_Name = name;
        UiTData->m_Width = stod(width);
        UiTData->m_Height = stod(height);
        UiTData->m_Theta = stod(theta);
        dataCenter.m_UIDatas.push_back(UiTData);
    }

    // 模型数据
    ModelData* MData = dataCenter.GetModelData(name);
    if(!MData)
    {
        // 创建数据
        MData = new ModelData();
        MData->m_Name = name;
        dataCenter.m_ModelDatas.push_back(MData);
    }

    // 设计
    if(!Design::TrapezoidDesign(*MData, UiTData))
    {
        cout << "Trapezoid design failed." << endl;
        return;
    }
    // 建模
    OCCTModel& occtModel = OCCTModel::getInstance();
    occtModel.m_Data = MData;
    TopoDS_Shape shape = occtModel.DoModel();

    // 渲染
    VTKRenderer& renderer = VTKRenderer::getInstance();
    renderer.AddModel(shape);
}

void CommandReceiver::executeExtrude(
    const string &name, const string &source, const string &distance)
{
    DataCenter& dataCenter = DataCenter::getInstance();

    // UI数据
    UIExtrudeData* UiEData = dynamic_cast<UIExtrudeData*>(dataCenter.GetUIData(name));    
    if(UiEData)
    {
        // 更新数据即可
        UiEData->m_Source = source;
        UiEData->m_Dis = stod(distance);
    }
    else
    {
        // 创建数据
        UiEData = new UIExtrudeData();
        UiEData->m_Name = name;
        UiEData->m_Source = source;
        UiEData->m_Dis = stod(distance);
        dataCenter.m_UIDatas.push_back(UiEData);
    }

    // 模型数据
    ModelData* MData = dataCenter.GetModelData(name);
    if(!MData)
    {
        // 创建数据
        MData = new ModelData();
        MData->m_Name = name;
        dataCenter.m_ModelDatas.push_back(MData);
    }

    // 设计
    if(!Design::ExtrudeDesign(*MData, UiEData))
    {
        cout << "Trapezoid design failed." << endl;
        return;
    }

    // 建模
    OCCTModel& occtModel = OCCTModel::getInstance();
    occtModel.m_Data = MData;
    TopoDS_Shape shape = occtModel.DoModel();

    // 渲染
    VTKRenderer& renderer = VTKRenderer::getInstance();
    renderer.AddModel(shape);
}

void CommandReceiver::executeShow()
{  
    // 渲染
    VTKRenderer& renderer = VTKRenderer::getInstance();
    renderer.Rendering();
}

void CommandReceiver::executeClear()
{
    // 清除
    VTKRenderer& renderer = VTKRenderer::getInstance();
    renderer.ClearAllModels();
}